package com.reremouse.rerebbs.web.mb.mag;

import com.reremouse.model.PartnerModel;
import com.reremouse.model.PartnerType;
import com.reremouse.rerebbs.dao.IPartnerDAO;
import com.reremouse.rerebbs.dao.IPartnerTypeDAO;
import com.reremouse.util.SpringHelper;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ManagedBean
@ViewScoped
public class ListPartner
        implements Serializable {
    List<PartnerModel> partnerList = new ArrayList();
    List<PartnerType> typeList = new ArrayList();
    PartnerType pt = new PartnerType();
    IPartnerDAO partnerDAO = (IPartnerDAO) SpringHelper.getSpringBean("PartnerDAO");
    IPartnerTypeDAO partnerTypeDAO = (IPartnerTypeDAO) SpringHelper.getSpringBean("PartnerTypeDAO");
    Map partnerMap = new HashMap();

    public List<PartnerModel> getPartnerList() {
        return this.partnerList;
    }

    public void setPartnerList(List<PartnerModel> partnerList) {
        this.partnerList = partnerList;
    }

    public PartnerType getPt() {
        return this.pt;
    }

    public void setPt(PartnerType pt) {
        this.pt = pt;
    }

    public List<PartnerType> getTypeList() {
        return this.typeList;
    }

    public void setTypeList(List<PartnerType> typeList) {
        this.typeList = typeList;
    }

    public Map getPartnerMap() {
        return this.partnerMap;
    }

    public void setPartnerMap(Map partnerMap) {
        this.partnerMap = partnerMap;
    }

    @PostConstruct
    public void init() {
        synchronizeDB();
        synType();
    }

    public void delete(ActionEvent e) {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String id = req.getParameter("id");

        this.partnerDAO.deletePartnerModel(Long.parseLong(id));
        synchronizeDB();
    }

    public String deleteType() {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String tid = req.getParameter("tid");

        this.partnerTypeDAO.deletePartnerType(Long.parseLong(tid));
        synType();
        return null;
    }

    public String addType() {
        this.partnerTypeDAO.addPartnerType(this.pt);
        this.pt = new PartnerType();
        synType();
        return null;
    }

    public String batchUpdateType() {
        for (PartnerType p : this.typeList) {
            this.partnerTypeDAO.updatePartnerType(p);
        }
        synType();
        return null;
    }

    public void synType() {
        this.typeList = this.partnerTypeDAO.findAllPartnerType();
        this.partnerMap.clear();
        for (PartnerType p : this.typeList) {
            this.partnerMap.put(p.getId(), p.getName());
        }
    }

    public void synchronizeDB() {
        this.partnerList = this.partnerDAO.findAllPartnerModel();
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.mag.ListPartner

 * JD-Core Version:    0.7.0.1

 */